<?php

class Bannermodel extends CI_Model {

    function Bannermodel() {
        // Call the Model constructor
        parent::__construct();
    }

    function getGlobalBanner() {
        $tsql_callSP = '{call sp_getGlobalBanner(?)}';

        $params = array(
            array($this->config->item('config_language_id'), SQLSRV_PARAM_IN)
        );

        $results = $this->db->exec_query($tsql_callSP, $params);

        $data = array();

        do {
            while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) {
                $data[] = $row;
            }
        } while (sqlsrv_next_result($results));

        return $data;
    }

    function generateGlobalBanner() {
        $this->load->helper('file_helper');

        $dir = $this->config->item('upload_media_folder_banner') . 'Global/';
        $data['global_images'] = read_dir($dir);

        $html = "";

        if (count($data['global_images']) > 0) {
            foreach ($data['global_images'] as $img) {
                $html .= "<div id=\"side_bar_content\">";
                $html .= "<img src=\"" . $this->config->item('media_folder_banner') . "Global/" . $img . "\" />"; //banner img
                $html .= "</div>";
            }
        }

        return $html;
    }

    function getbanner($id) {
        $query = $this->db->query("SELECT * FROM bannergroup WHERE idbannergroup = " . $this->db->escape($id));

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    
    function getChild($banneritem) {
    	$html = "";
		
		if (isset($banneritem->Link ) && $banneritem->Link != "" && !is_null($banneritem->Link) && ($banneritem->Name == "Video")) {
			$html .= "<div class=\"w-box-content-2\">";
			$html .= $banneritem->Link; //append anchor to banner
			$html .= "</div>";
		} else {
			$html .= "<div class=\"w-box-content-2\">";
			$html .= "<a href=\"" . $banneritem->Link . "\" title=\"" . $banneritem->FileName . "\" target=\"_blank\"><img src=\"" . $this->config->item('HTTP_MEDIA_BANNER') . $banneritem->FileName . "\" /></a>";
			$html .= "</div>";
		}
		
		return $html;
    }
    
    function getExtensions($page, $market, $language){
   		$str = "select b.*, bg.Name, pb.SortOrder
				from dbo.Pages p
				left join dbo.PageBanner pb ON p.Page_ID = pb.PageFID
				left join dbo.Banner b ON b.Banner_ID = pb.BannerFID
				left join dbo.BannerGroup bg ON bg.BannerGroup_ID = b.BannerGroupFID
				WHERE p.SystemName = '". $this->db->escape_str($page) ."' and p.MarketFID = '". $this->db->escape_str($market) ."' AND b.LanguageFID = ". $this->db->escape($language) ."";
    	
    	$query = $this->db->query($str);
    	
    	
        
        $data = array();
        
        if ($query->num_rows() > 0) {
        	foreach ($query->result() as $banneritem) {
	            $module = $this->getChild($banneritem);
	
	            if ($module) {
	                $data[] = $module;
	            }
	        }
        }
        
        return $data;
    }
    
    function getBanners($page = 'Home', $market, $language) {
    	return $this->getExtensions($page, $market, $language);
    }

    function getbannerblock_html($id) {
        $query = $this->db->query("SELECT * FROM bannergroup WHERE idbannergroup = " . $this->db->escape($id));
        $html = "";



        if ($query->num_rows() > 0) {


            $bannerblock = $query->row();
            $banneritems = $this->getbanneritemsby_bannerid($bannerblock->idbannergroup);

            if ($banneritems->num_rows() > 0) {

                $html = "<div class=\"side_block_item " . $bannerblock->type . "\">"; //start banner block
                foreach ($banneritems->result() as $banneritem) {

                    $html .= "<div class=\"rotating_item\">"; //start div banner

                    if ($banneritem->link != "" && !is_null($banneritem->link)) {

                        $html .= "<a href=\"" . $banneritem->link . "\" title=\"" . $banneritem->title . "\">"; //append anchor to banner
                    }

                    $html .= "<img src=\"" . $this->config->item('media_folder_banner') . $banneritem->banner_url . "\" />"; //banner img

                    if ($banneritem->showdetails == "1") {
                        $html .= "<div class=\"banner_text\">"; //banner details start div

                        $html .= "<p class=\"head\">" . $banneritem->title . "</p>"; //banner title

                        $html .= "<p class=\"details\">" . $banneritem->description . "</p>"; //banner description

                        $html .= "</div>"; //banner details close div
                    }


                    if ($banneritem->link != "" && !is_null($banneritem->link)) {

                        $html .= "</a>"; //append close anchor tag
                    }

                    $html .= "</div>"; //close banner div
                }
                $html .= "</div>"; //close banner block
            }

            return $html;
        } else {
            return false;
        }
    }

    function getbanneritemsby_bannerid($id) {

        $query = $this->db->query("SELECT * FROM banner WHERE fkidbannergroup =  " . $this->db->escape($id) . " Order by sequence ASC, idBanner ASC");

        return $query;
    }

    function getbanneritem($id) {

        $query = $this->db->query("SELECT * FROM banner WHERE idBanner =  " . $this->db->escape($id));

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

}

?>